<?php

/**
 * Zend Framework (http://framework.zend.com/)
 *
 * @link      http://github.com/zendframework/ZendSkeletonModule for the canonical source repository
 * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
 * @license   http://framework.zend.com/license/new-bsd New BSD License
 */

namespace VeCongTy\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\Form\Form;
use Zend\Form\Element;

class ConfigController extends AbstractActionController {

    protected $userTable;
    protected $settingsTable;

    public function getUserTable() {
        if (!$this->userTable) {
            $this->userTable = $this->getServiceLocator()->get('User\Model\UserTable');
        }

        return $this->userTable;
    }
    
    public function getSettingsTable() {
        if (!$this->settingsTable) {
            $this->settingsTable = $this->getServiceLocator()->get('VeCongTy\Model\SettingsTable');
        }

        return $this->settingsTable;
    }

    public function indexAction() {

        if($this->request->isPost())
        {
            $nid = $this->params()->fromPost('user');
            $this->getSettingsTable()->saveAdmin(intval($nid));
        }
        
        $form = new Form('admin-setting');
        $form->setAttribute('method', 'POST');
        
        $resultSet = $this->getUserTable()->fetchAll();
        
        $values = array();
        
        foreach ($resultSet as $row) {
            $values[$row->getId()] = $row->getUsername();
        }
        
        $select = new Element\Select('user');
        $select->setLabel('Select user:');
        $select->setValueOptions($values);
        
        $submit = new Element\Submit('submit');
        $submit->setValue('Save');
        
        $form->add($select)->add($submit);
        
        return array('form' => $form);
    }

    public function setUpDbAction() {

        $adapter = $this->getServiceLocator()->get('Zend\Db\Adapter\Adapter');

        $schema = include __DIR__ . '/../../../db/schema.php';

        try {
            foreach ($schema['schema_up'] as $schemaStmt) {
                try {
                    $adapter->query($schemaStmt, $adapter::QUERY_MODE_EXECUTE);
                } catch (\Exception $e) {
                    echo $schemaStmt . ' FAILED DUE TO ' . $e->getMessage() . PHP_EOL;
                }
            }
        } catch (\Exception $e) {
            echo $e->getMessage();
            exit(1);
        }

        $this->redirect()->toRoute('vecongty/process', array('controller' => 'config', 'action' => 'index'));
    }

    public function setDownDbAction() {

        $adapter = $this->getServiceLocator()->get('Zend\Db\Adapter\Adapter');

        $schema = include __DIR__ . '/../../../db/schema.php';

        try {
            foreach ($schema['schema_down'] as $schemaStmt) {
                try {
                    $adapter->query($schemaStmt, $adapter::QUERY_MODE_EXECUTE);
                } catch (\Exception $e) {
                    echo $schemaStmt . ' FAILED DUE TO ' . $e->getMessage() . PHP_EOL;
                }
            }
        } catch (\Exception $e) {
            echo $e->getMessage();
            exit(1);
        }

        $this->redirect()->toRoute('vecongty/process', array('controller' => 'config', 'action' => 'index'));
    }

}
